package org.dromara.system.mq;

import org.dromara.common.core.utils.DateUtils;
import org.dromara.system.mq.config.EmailQueueConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EmailSender {
    private static final Logger log = LoggerFactory.getLogger(EmailSender.class);

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendEmailMessage(Long queueId, long delayTime) {
//        log.info("===============触发延时发送邮件==={}=================\n发送时间:{}\n{}ms后执行", queueId, DateUtils.getDate(), delayTime);
        try {
            amqpTemplate.convertAndSend(EmailQueueConfig.EMAIL_DELAYED_EXCHANGE_NAME, EmailQueueConfig.EMAIL_DELAYED_KEY, queueId, message -> {
                message.getMessageProperties().setDelayLong(delayTime);
                return message;
            });
        } catch (AmqpException e) {
            log.error("延时发送邮件 异常{},{}", getClass(), e.getMessage());
        }
    }

}
